<template>
  <el-dialog
    :title="!dataForm.housingId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小区ID" prop="communityId">
      <el-input v-model="dataForm.communityId" placeholder="小区ID"></el-input>
    </el-form-item>
    <el-form-item label="房源标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="房源标题"></el-input>
    </el-form-item>
    <el-form-item label="房屋面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="房屋面积"></el-input>
    </el-form-item>
    <el-form-item label="售价/租金" prop="money">
      <el-input v-model="dataForm.money" placeholder="售价/租金"></el-input>
    </el-form-item>
    <el-form-item label="排序（从高到低）" prop="level">
      <el-input v-model="dataForm.level" placeholder="排序（从高到低）"></el-input>
    </el-form-item>
    <el-form-item label="状态（1正常，2禁用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态（1正常，2禁用）"></el-input>
    </el-form-item>
    <el-form-item label="类型(1出租 2二手出售)" prop="type">
      <el-input v-model="dataForm.type" placeholder="类型(1出租 2二手出售)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          housingId: 0,
          communityId: '',
          title: '',
          area: '',
          money: '',
          level: '',
          status: '',
          type: ''
        },
        dataRule: {
          communityId: [
            { required: true, message: '小区ID不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '房源标题不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '房屋面积不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '售价/租金不能为空', trigger: 'blur' }
          ],
          level: [
            { required: true, message: '排序（从高到低）不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（1正常，2禁用）不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '类型(1出租 2二手出售)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.housingId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.housingId) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/housing/info/${this.dataForm.housingId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.communityId = data.housing.communityId
                this.dataForm.title = data.housing.title
                this.dataForm.area = data.housing.area
                this.dataForm.money = data.housing.money
                this.dataForm.level = data.housing.level
                this.dataForm.status = data.housing.status
                this.dataForm.type = data.housing.type
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/housing/${!this.dataForm.housingId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'housingId': this.dataForm.housingId || undefined,
                'communityId': this.dataForm.communityId,
                'title': this.dataForm.title,
                'area': this.dataForm.area,
                'money': this.dataForm.money,
                'level': this.dataForm.level,
                'status': this.dataForm.status,
                'type': this.dataForm.type
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
